package de.joergviola.mobj.client;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;

import de.joergviola.mobj.shared.KeyNotUniqueException;
import de.joergviola.mobj.shared.ObjectNotFoundException;
import de.joergviola.mobj.shared.Persistable;
import de.joergviola.mobj.shared.Query;
import de.joergviola.mobj.shared.UnknownEntityException;

public interface ITransaction {

	/**
	 * Create an object in the datastore.
	 * 
	 * @param t
	 *            Object to be created
	 * @param callback
	 *            Called when operation finishes
	 * @returns A copy of object t or the object itself, with its id set
	 * @throws UnknownEntityException
	 *             This class was not registered in the schema
	 */
	<T extends Persistable> void insert(T t, AsyncCallback<T> callback);

	/**
	 * Update an object already existing in the datastore.
	 * 
	 * @param t
	 *            Object to be updated
	 * @param callback
	 *            Called when operation finishes
	 * @throws UnknownEntityException
	 *             This class was not registered in the schema
	 */
	<T extends Persistable> void update(T t, AsyncCallback<Void> callback);

	/**
	 * Get an object from the datastore by its id. Typically used to resolve
	 * to-one references.
	 * 
	 * @param className
	 *            The name of the objects class - use t.getClass().getName()
	 * @param id
	 *            The id of the object requested
	 * 
	 * @param callback
	 *            Called when operation finishes
	 * @returns The object stored for the id given. Throws an exception when to
	 *          id was not yet stored.
	 * 
	 * @throws ObjectNotFoundException
	 *             If no object was stored for this id
	 * @throws KeyNotUniqueException
	 *             If this id was used more than once - datastore should be
	 *             considered corrupted then
	 * @throws UnknownEntityException
	 *             This class was not registered in the schema
	 */
	<T extends Persistable> void get(String className, Long id,
			AsyncCallback<T> callback);

	/**
	 * Get objects from the datastore by their ids. Typically used to resolve
	 * to-many references.
	 * 
	 * @param className
	 *            The name of the objects class - use t.getClass().getName()
	 * @param ids
	 *            The ids of the objects requested
	 * 
	 * @param callback
	 *            Called when operation finishes
	 * @returns The objects stored for the ids given in the same order. Throws
	 *          an exception when one of the ids was not yet stored.
	 * 
	 * @throws ObjectNotFoundException
	 *             If no object was stored for one of the ids. The callback is
	 *             called one for any of the missing objects.
	 * @throws KeyNotUniqueException
	 *             If one of the ids was used more than once - datastore should
	 *             be considered corrupted then. The callback is called one for
	 *             any of the duplicate keys.
	 * @throws UnknownEntityException
	 *             This class was not registered in the schema
	 */
	<T extends Persistable> void get(String className, List<Long> id,
			AsyncCallback<List<T>> callback);

	/**
	 * Get objects from the datastore by querying attributs.
	 * 
	 * @param className
	 *            The name of the objects class - use t.getClass().getName()
	 * @param query
	 *            The query used to find objects.
	 * 
	 * @param callback
	 *            Called when operation finishes
	 * @returns The objects matching the query in the any order.
	 * 
	 * @throws UnknownEntityException
	 *             This class was not registered in the schema
	 */
	<T extends Persistable> void find(String className, Query query,
			AsyncCallback<List<T>> callback);

	/**
	 * Delete an object from the datastore.
	 * 
	 * @param t
	 *            Object to be deleted.
	 * @param callback
	 *            Called when operation finishes
	 * @throws UnknownEntityException
	 *             This class was not registered in the schema
	 */
	<T extends Persistable> void delete(T t, AsyncCallback<Void> callback);

}
